# coding: utf-8
import sys
import os
rootdir = os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir))
sys.path.append(rootdir)
from flask import Flask, request,jsonify
from flask_restful import Resource, Api
import Interface.quant_query_interface as query
import json
app = Flask(__name__,static_url_path='')
app.config['JSON_AS_ASCII'] = False
api = Api(app)

todos = {
    'todo1': {'task': 'build an API'},
    'todo2': {'task': '?????'},
    'todo3': {'task': 'profit!'},
}

class HelloWorld(Resource):
    def get(self):
        return {'hello': 'world'}

class TodoSimple(Resource):
    def get(self, todo_id):
        return {todo_id: todos[todo_id]}

    def put(self, todo_id):
        todos[todo_id] = request.form['data']
        return {todo_id: todos[todo_id]}


@app.route('/stock', methods=['GET'])
def stock_form():
    return '''<form action="/stock" method="post">
                      <p>查询股票代码（000001.SZ）<input name="stock_code"></p>
                      <p>查询起始时间（2013-02-04）<input name="start_time"></p>
                      <p>查询结束时间（2018-04-04）<input name="end_time"></p>
                      <p>查询<button type="submit">query</button></p>
              </form>'''

@app.route('/stock', methods=['POST'])
def signin():
    # 需要从request对象读取表单内容：
    stock_code = request.form['stock_code']
    start_time = request.form['start_time']
    end_time=request.form['end_time']
    index_stock_code='hs300'
    result=query.get_stock_quant(stock_code,index_stock_code,start_time,end_time)
    result['stock_code']=stock_code
    result['start_time']=start_time
    result['end_time']=end_time
    return jsonify(result)



api.add_resource(HelloWorld, '/')
api.add_resource(TodoSimple, '/<string:todo_id>')

if __name__ == '__main__':
    app.run(debug=True)